/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managers;

import conexionBD.ConexionServidor;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

/**
 *
 * @author ruanda
 */
@ManagedBean
@SessionScoped
public class managerAuditoria implements Serializable {

    private String numeroExpe;
 
    FacesContext context;

    /** Creates a new instance of managerControlFechas */
    public managerAuditoria() {
    }

  

    public String getNumeroExpe() {
        return numeroExpe;
    }

    public void setNumeroExpe(String numeroExpe) {
        this.numeroExpe = numeroExpe;
    }

   

    public String limpiar() {
        String destino = "hacia-inicio";
        numeroExpe = "";
      
        return destino;
    }

    public Boolean hayResultados() throws Exception {
        ConexionServidor miConexion = new ConexionServidor();
        ResultSet rs = null;
        Boolean isNotEmpty = null;
        try {
            Connection con = miConexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from bitacora_cambio where idexpediente = ?");
            ps.setString(1, numeroExpe);
            rs = ps.executeQuery();
            isNotEmpty = rs.next();
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return isNotEmpty;
    }

    public String buscandoInfo() throws Exception {
        String destino = null;
        if (hayResultados()) {
        } else {
            context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "BUSQUEDA EXPEDIENTE", "El expediente ingresado no existe o no posee registros de auditoria"));
            limpiar();
        }
        return destino;
    }

    public Result getListaExpediente() throws Exception {
        ConexionServidor miConexion = new ConexionServidor();
        Connection conn = miConexion.getConnection();
        try {
            PreparedStatement ps = conn.prepareStatement("select * from bitacora_cambio where idexpediente = ?");
            ps.setString(1, numeroExpe);
            ResultSet rs = ps.executeQuery();
            return ResultSupport.toResult(rs);
        } finally {
            conn.close();
        }
    }
}
